import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import pandas as pd

url1 = 'https://s.weibo.com/top/summary'
url2 = 'https://s.weibo.com/top/summary?cate=realtimehot'
url3 = 'https://weibo.com'
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
# }

# response = requests.get(url2, headers=headers)
# soup = BeautifulSoup(response.text, 'html.parser')
# print(soup.prettify())


# 打开浏览器
service = Service('c:\python312\Scripts\chromedriver.exe')
browser = webdriver.Chrome(service=service)
browser.maximize_window()
browser.get(url2)
time.sleep(1)

content = browser.page_source
soup = BeautifulSoup(content, 'html.parser')
titles = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > a')
ranks = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-01')
searchs = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > span')

titleList = []
rankList = []
searchList = []
for item in titles:
    titleList.append(item.get_text())

for item in ranks:
    rankList.append(item.get_text())


for item in searchs:
    searchList.append(item.get_text())
all_content = list(zip(rankList,titleList , searchList))
# print(all_content)
df = pd.DataFrame(all_content, columns=['排名', '标题', '搜索'])
df.to_excel('微博热门话题.xlsx', index=False)

# print(ranks)
browser.quit()
